package com.cenntro.nearr.sms;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.tomcat.util.security.MD5Encoder;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cenntro.nearr.sms.xsend.AppConfig;
import com.cenntro.nearr.sms.xsend.ConfigLoader;
import com.cenntro.nearr.sms.xsend.MessageMultiXSend;

public class SmsUtil {

	private static org.slf4j.Logger LOG = LoggerFactory.getLogger(SmsUtil.class);

	private static String appid = "60185";
	private static String appkey = "2e89ab325667c5895be9c0a5124d9e58";
	private static String url1 = "https://api.mysubmail.com/internationalsms/xsend.json";
	private static String url2 = "https://api.mysubmail.com/internationalsms/send.json";

	/**
	 * 
	 */
	public static void send(String param) {
		AppConfig config = ConfigLoader.load(ConfigLoader.ConfigType.Message);
		MessageMultiXSend submail = new MessageMultiXSend(config);
		submail.addProject("g8crk1");
		JSONObject json = new JSONObject();
		json.put("name", "张三");
		json.put("code", "1123");
		json.put("time", "1分钟");
		submail.addVars(json);
		submail.addMulti("+18252512040");

		JSONObject json2 = new JSONObject();
		json2.put("name", "老江");
		json2.put("code", "11244");
		json2.put("time", "1分钟");
		submail.addVars(json2);
		submail.addMulti("+18252512040");
		String response = submail.multixsend();
		System.out.println("接口返回消息:" + response);
	}

	private static String getParam(String to, String content) {
		String str = "";
		str += "appid=" + appid;
		str += "&to=" + to;
		str += "&content=" + content;
		str += "&signature=" + appkey;
		LOG.info("signature= {}", str);
		// String signature = "";
		// try {
		// MessageDigest md5 = MessageDigest.getInstance("MD5");
		// byte[] digest = md5.update(str.getBytes());
		//
		// signature = new String(digest);
		// System.out.println(signature);
		// } catch (Exception e) {
		// LOG.error("UnsupportedEncodingException", e);
		// }
		// str += "&signature=" + signature;
		LOG.info("parma= {}", str);
		return str;
	}

	// 14:17:21.828 [main] INFO com.cenntro.nearr.sms.SmsUtil - parma=
	// appid=60185&to=18106530060&vars={"code":"356123"}&appkey=2e89ab325667c5895be9c0a5124d9e58&signature=$1$vxY3S/wk$yWO/JAHx8W6x1jV1cQnjO0
	public static void main(String[] args) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("code", "356123");

		String str = getParam("18106530060", "【SUBMAIL】您的短信验证码：4438，请在10分钟内输入。");
		send(str);
	}

}
